<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
    <title>ShellCheck: SC2152 – Can only return 0-255. Other data should be written to stdout.</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
  </head>
  <body style="margin-left: auto; margin-right: auto; max-width: 800px">
    <h1>SC2152 – ShellCheck Wiki</h1>
    <a href="https://github.com/koalaman/shellcheck/wiki/SC2152">See this page on GitHub</a>
    <p style="display: none"><a href="index.html">Sitemap</a></p>
    <hr />
    <h2
id="can-only-return-0-255-other-data-should-be-written-to-stdout">Can
only return 0-255. Other data should be written to stdout.</h2>
<h3 id="problematic-code">Problematic code:</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="SC2152.html#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">myfunc()</span> <span class="kw">{</span></span>
<span id="cb1-2"><a href="SC2152.html#cb1-2" aria-hidden="true" tabindex="-1"></a>  <span class="cf">return</span> <span class="st">&quot;Hello </span><span class="va">$USER</span><span class="st">&quot;</span></span>
<span id="cb1-3"><a href="SC2152.html#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="kw">}</span></span></code></pre></div>
<h3 id="correct-code">Correct code:</h3>
<div class="sourceCode" id="cb2"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="SC2152.html#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="fu">myfunc()</span> <span class="kw">{</span></span>
<span id="cb2-2"><a href="SC2152.html#cb2-2" aria-hidden="true" tabindex="-1"></a>  <span class="bu">echo</span> <span class="st">&quot;Hello </span><span class="va">$USER</span><span class="st">&quot;</span></span>
<span id="cb2-3"><a href="SC2152.html#cb2-3" aria-hidden="true" tabindex="-1"></a>  <span class="cf">return</span> <span class="dv">0</span></span>
<span id="cb2-4"><a href="SC2152.html#cb2-4" aria-hidden="true" tabindex="-1"></a><span class="kw">}</span></span></code></pre></div>
<h3 id="rationale">Rationale:</h3>
<p>In many languages, <code>return</code> is used to return from the
function with a final result.</p>
<p>In sh/bash, <code>return</code> can only be used to signal success or
failure (0 = success, 1-255 = failure), more akin to
<code>throw/raise</code> in other languages.</p>
<p>Results should instead be written to stdout and captured:</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb3-1"><a href="SC2152.html#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="va">message</span><span class="op">=</span><span class="va">$(</span><span class="ex">myfunc</span><span class="va">)</span></span>
<span id="cb3-2"><a href="SC2152.html#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="bu">echo</span> <span class="st">&quot;The function wrote: </span><span class="va">$message</span><span class="st">&quot;</span></span></code></pre></div>
<p>In functions that return small integers, such as getting the cpu
temperature, the value should still be written to stdout.
<code>return</code> should be reserved for error conditions, such as
"can't determine CPU temperature". Error or failure messages should be
written to stderr.</p>
<p>Note in particular that <code>return -1</code> is equivalent to
<code>return 255</code>, but that <code>return 1</code> is the more
canonical way of expressing the first possible error code.</p>
<h3 id="exceptions">Exceptions:</h3>
<p>None</p>
<h3 id="related-resources">Related resources:</h3>
<ul>
<li><a href="https://mywiki.wooledge.org/BashFAQ/084">BashFaq: How do I
return a string (or large number, or negative number) from a
function?</a> "return" only lets me give a number from 0 to 255.</li>
</ul>
    <hr />
    <p style='font-size: 80%'><a href="../index.html">ShellCheck</a> is a static analysis tool for shell scripts. This page is part of its documentation.</p>
  </body>
</html>


